package com.vshop.service.alast.impl;

import com.vshop.config.dto.StringOutputConstant;
import com.vshop.entity.agatewaytobeijing.querydevice.beijing.response.QueryDeviceData;
import com.vshop.entity.agatewaytobeijing.querydevice.beijing.response.ResponseQueryDevice;
import com.vshop.entity.agatewaytobeijing.queryh5order.response.H5PackageOrder;
import com.vshop.entity.agatewaytobeijing.queryh5order.response.ResponseQueryH5Order;
import com.vshop.entity.optimize.cancel.response.CancelResponse;
import com.vshop.entity.optimize.end.response.EndResponse;
import com.vshop.entity.optimize.order.response.OrderResponse;
import com.vshop.entity.optimize.reorder.response.RenewResponse;
import com.vshop.entity.purchase.request.RequestPurchaseService;
import com.vshop.entity.requestinterface.Constant;
import com.vshop.service.agatewaytobeijing.GatewayToBeijingService;
import com.vshop.service.alast.OriginalOrderServicePlanService;
import com.vshop.service.impl.PurchaseLogicService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 未升级前2.0后的版本：异步订购和异步回调 ：：业务实现处理类
 *
 * @author lm
 * @date 2021/6/25 17:33
 */
@Service
@Slf4j
public class OriginalOrderServicePlanServiceImpl implements OriginalOrderServicePlanService {
    @Autowired
    PurchaseLogicService purchaseService;
    @Autowired
    OrderAssistService assistService;
    @Autowired
    GatewayToBeijingService toBeijingService;

    /**
     * 旧的：：：：：
     * 2.0版本接口：订购套餐业务逻辑处理类
     * <p>
     * 订购套餐的操作：
     * 1，先判断1M赠送套餐（有：判断状态：a，正在使用==提前结束套餐；b==取消套餐；无：继续走第2步）
     * <p>
     * 2，再判断正常套餐（有：判断状态：
     * 21，有正在使用订单：a1，与正在使用套餐编码一致==续订；a2，与正在使用订单不一致，与未启用订单一致==续订未启用订单；
     * 无：继续走第22步；
     * 22，无正在使用订单时：a1，与暂停订单套餐编码一致==续订；a2，与未启用订单套餐编码一致==续订
     * 无正常订单：继续走第3步：）
     * <p>
     * 3，新订购套餐
     * 续订接口和新订接口都是及时响应的
     *
     * @param pur
     * @return
     */
    @Override
    public String purchaseServicePlan(RequestPurchaseService pur) {

        /*说明参数是没有问题的，现在开始做正事*/
        log.info(pur.getDeviceCode() + "：查询【所有订单】");
        /*ResponsePackageQueryNew zsData = toBeijingService.queryOrder(pur.getDeviceCode(), "", "", "", pur.getPartnerCode(), pur.getSecret());*/
        ResponseQueryH5Order zsData = toBeijingService.queryOrder(pur.getDeviceCode(), "", "", "", pur.getPartnerCode(), pur.getSecret());

        //1，先去拿到卡片上面所有订单，匹配赠送套餐（有：判断状态：a，正在使用==提前结束套餐；b==取消套餐；无：继续走第2步）
        matchGiveDate(zsData, pur);

        //2,匹配续订根据结果判断接下来要走的路
        String result = matchRenewOrder(zsData, pur);

        //3,如果result返回的是“SUCCESS”，就继续走新订路
        if (StringUtils.isNotBlank(result) && StringOutputConstant.CODE_SUCCESS.equals(result)) {
            return matchOrder(zsData, pur);
        }

        //4，如果result返回的不是“SUCCESS”，那就可以直接返回续订的结果
        return result;
    }

    /**
     * 旧的：匹配赠送套餐的逻辑
     * <p>
     * 1,未启用-取消套餐
     * <p>
     * 2，正在使用-结束套餐
     *
     * @param zsData
     * @param pur
     */
    public void matchGiveDate(ResponseQueryH5Order zsData, RequestPurchaseService pur) {

        if (null != zsData && Constant.RET_CODE_0000.equals(zsData.getCode()) && null != zsData.getData() && null != zsData.getData().getPackage_order() && !zsData.getData().getPackage_order().isEmpty()) {

            for (H5PackageOrder tr : zsData.getData().getPackage_order()) {
                if (StringUtils.isNotBlank(tr.getPackageCode()) && StringUtils.isNotBlank(tr.getOrderCode()) && StringUtils.isNotBlank(tr.getOrderStatus())) {
                    //拿着tr.getPackageCode()去数据库查询看看有没有这个赠送套餐的套餐编码
                    Integer c = purchaseService.queryGiveDateOrNot(tr.getPackageCode());
                    log.info(tr.getPackageCode() + "：匹配【赠送套餐】，结果是：" + c);
                    //赠送套餐且为未启用状态
                    boolean b1 = (StringOutputConstant.CODE_ONE.equals(tr.getOrderStatus()) && c >= 1);
                    //赠送套餐且为正在使用状态
                    boolean b2 = (StringOutputConstant.CODE_TWO.equals(tr.getOrderStatus()) && c >= 1);
                    if (b1) {
                        log.info(pur.getDeviceCode() + "：查询到有【未启用赠送套餐】：" + tr.getPackageName());
                        //未启用赠送套餐----取消订单
                        CancelResponse cancel = toBeijingService.cancel(tr.getOrderCode(), pur.getPartnerCode(), pur.getSecret());
                        if (null != cancel && StringUtils.isNotBlank(cancel.getCode()) && Constant.RET_CODE_0000.equals(cancel.getCode())) {
                            log.info(pur.getDeviceCode() + "：未启用赠送套餐订单：" + tr.getOrderCode() + "取消成功");
                        } else {
                            log.info(pur.getDeviceCode() + "：未启用赠送套餐订单：" + tr.getOrderCode() + "取消失败，发送异常邮件");
                            if (null == cancel) {
                                //发送异常邮件：
                                purchaseService.sendMail(pur.getDeviceCode(), pur.getPackageName(), tr.getOrderCode(), "取消订单", "接口连接超时或其他原因导致的取消失败");
                                //记录异常操作到数据库：
                                assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode(), pur.getPayId(), pur.getOrderNo(), "3", "9998", "程序中异常导致的失败");
                                log.info(pur.getDeviceCode() + "：【未启用赠送套餐订单】接口连接超时或其他原因导致的取消失败");
                            } else {
                                if (StringUtils.isNotBlank(cancel.getCode())) {
                                    //发送异常邮件：
                                    purchaseService.sendMail(pur.getDeviceCode(), pur.getPackageName(), tr.getOrderCode(), "取消订单", cancel.getMessage());
                                    //记录异常操作到数据库：
                                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode(), pur.getPayId(), pur.getOrderNo(), "3", cancel.getCode(), cancel.getMessage());
                                    log.info(pur.getDeviceCode() + "：【未启用赠送套餐订单】取消失败，原因：" + cancel.getMessage());
                                } else {
                                    //发送异常邮件：
                                    purchaseService.sendMail(pur.getDeviceCode(), pur.getPackageName(), tr.getOrderCode(), "取消订单", "接口连接超时或其他原因导致的取消失败");
                                    //记录异常操作到数据库：
                                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode(), pur.getPayId(), pur.getOrderNo(), "3", "9998", "程序中异常导致的失败");
                                    log.info(pur.getDeviceCode() + "：【未启用赠送套餐订单】接口连接超时或其他原因导致的取消失败");
                                }
                            }
                        }
                    } else if (b2) {
                        log.info(pur.getDeviceCode() + "：查询到有【正在使用赠送套餐】：" + tr.getPackageName());
                        //正在使用赠送套餐----提前结束订单
                        EndResponse end = toBeijingService.end(tr.getOrderCode(), pur.getPartnerCode(), pur.getSecret(), "购买新套餐时需将赠送套餐提前结束");
                        if (null != end && StringUtils.isNotBlank(end.getCode()) && Constant.RET_CODE_0000.equals(end.getCode())) {
                            log.info(pur.getDeviceCode() + "：正在使用赠送套餐订单：" + tr.getOrderCode() + "【提前结束】成功");
                        } else {
                            log.info(pur.getDeviceCode() + "：正在使用赠送套餐订单：" + tr.getOrderCode() + "【提前结束】失败，发送异常邮件");
                            if (null == end) {
                                //发送异常邮件：
                                purchaseService.sendMail(pur.getDeviceCode(), pur.getPackageName(), tr.getOrderCode(), "提前结束订单", "接口连接超时或其他原因导致的提前结束失败");
                                //记录异常操作到数据库：
                                assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode(), pur.getPayId(), pur.getOrderNo(), "4", "9998", "程序中异常导致的失败");
                                log.info(pur.getDeviceCode() + "：【正在使用赠送套餐订单】因接口连接超时或其他原因导致的提前结束失败");
                            } else {
                                if (StringUtils.isNotBlank(end.getCode())) {
                                    //发送异常邮件：
                                    purchaseService.sendMail(pur.getDeviceCode(), pur.getPackageName(), tr.getOrderCode(), "提前结束订单", end.getMessage());
                                    //记录异常操作到数据库：
                                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode(), pur.getPayId(), pur.getOrderNo(), "4", end.getCode(), end.getMessage());
                                    log.info(pur.getDeviceCode() + "：【正在使用赠送套餐订单】提前结束失败，原因：" + end.getMessage());
                                } else {
                                    //发送异常邮件：
                                    purchaseService.sendMail(pur.getDeviceCode(), pur.getPackageName(), tr.getOrderCode(), "提前结束订单", "接口连接超时或其他原因导致的提前结束失败");
                                    //记录异常操作到数据库：
                                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode(), pur.getPayId(), pur.getOrderNo(), "4", "9998", "程序中异常导致的失败");
                                    log.info(pur.getDeviceCode() + "：【正在使用套餐订单】因接口连接超时或其他原因导致的提前结束失败");
                                }
                            }
                        }
                    }
                }
            }
        } else {
            //发送异常邮件：
            purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "查询全部订单", "未查询到任何套餐，请求或相应出现异常！");
            //记录异常操作到数据库：
            assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode(), pur.getPayId(), pur.getOrderNo(), "4", "9999", "未查询到任何套餐，请求或相应出现异常！");
            log.info(pur.getDeviceCode() + "未查询到任何套餐，请求或相应出现异常！");
        }
    }

    /**
     * 旧的：匹配存在续订套餐可能的方法
     * <p>
     * 2，再判断正常套餐（有：判断状态：
     * 21，有正在使用订单：a1，与正在使用套餐编码一致==续订；a2，与正在使用订单不一致，与未启用订单一致==续订未启用订单；
     * 无：继续走第22步；
     * 22，无正在使用订单时：a1，与暂停订单套餐编码一致==续订；a2，与未启用订单套餐编码一致==续订
     * 无正常订单：继续走第3步：）
     *
     * @param zsData
     * @param pur
     * @return
     */
    public String matchRenewOrder(ResponseQueryH5Order zsData, RequestPurchaseService pur) {

        if (StringUtils.isNotBlank(pur.getBuyType()) && pur.getBuyType().equals(StringOutputConstant.CODE_TWO)) {
            //orderType :订单类型：0-日程套餐订单；1-流量套餐订单；
            //orderStatus :订单状态：0-未支付；1-未启用；2-开始使用；3-已结束；4-已取消；5-已暂停；
            log.info(pur.getDeviceCode() + "：查询【正在使用订单】");
            /*ResponsePackageQueryNew inUse = toBeijingService.queryOrder(pur.getDeviceCode(), "", "2", "1", pur.getPartnerCode(), pur.getSecret());*/
            ResponseQueryH5Order inUse = toBeijingService.queryOrder(pur.getDeviceCode(), "", "2", "1", pur.getPartnerCode(), pur.getSecret());

            if (null != inUse && Constant.RET_CODE_0000.equals(inUse.getCode()) && null != inUse.getData() && null != inUse.getData().getPackage_order() && !inUse.getData().getPackage_order().isEmpty()) {
                for (H5PackageOrder tr : inUse.getData().getPackage_order()) {
                    if (StringUtils.isNotBlank(tr.getPackageCode()) && StringUtils.isNotBlank(tr.getOrderCode()) && StringUtils.isNotBlank(pur.getPackageCode())) {
                        if (pur.getPackageCode().equalsIgnoreCase(tr.getPackageCode())) {
                            log.info(pur.getDeviceCode() + "：查到与【正在使用】的订单code一致：【续订】");
                            RenewResponse reNew = toBeijingService.reOrder(tr.getOrderCode(), pur);
                            if (null != reNew && Constant.RET_CODE_0000.equals(reNew.getCode())) {
                                //去数据库更新isRenew和isOpen参数
                                Integer c = assistService.updateOrderByOrderNo(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "1");
                                if (c > 0) {
                                    return assistService.responseStr("0000", pur.getDeviceCode() + "：请求【续订正在使用单】成功");
                                } else {
                                    //发送异常邮件：
                                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订正在使用单", "请求【续订正在使用单】成功，但入库失败");
                                    return assistService.responseStr("9999", pur.getDeviceCode() + "：请求【续订正在使用单】成功，但入库失败");
                                }
                            } else {
                                if (null == reNew) {
                                    //发送异常邮件：
                                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订正在使用订单", "【续订正在使用单】程序中异常导致的失败");
                                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "2", "9998", "程序中异常导致的失败");
                                    return assistService.responseStr("9998", pur.getDeviceCode() + "：【续订正在使用单】失败");
                                } else {
                                    if (StringUtils.isNotBlank(reNew.getCode())) {
                                        //发送异常邮件：
                                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订正在使用订单", reNew.getMessage());
                                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "2", reNew.getCode(), reNew.getMessage());
                                        return assistService.responseStr(reNew.getCode(), pur.getDeviceCode() + ":" + reNew.getMessage());
                                    } else {
                                        //发送异常邮件：
                                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订正在使用订单", "【续订正在使用单】程序中异常导致的失败");
                                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "2", "9998", "程序中异常导致的失败");
                                        return assistService.responseStr("9998", pur.getDeviceCode() + "：【续订正在使用订单】失败");
                                    }
                                }
                            }
                        } else {
                            //正在使用的订单存在，但是与现在请求的套餐编码不一致，继续去查询是不是存在相同套餐的未启用订单
                            log.info(pur.getDeviceCode() + "：【有正在使用订单，再匹配未启用或暂停订单】");
                            if (null != zsData && Constant.RET_CODE_0000.equals(zsData.getCode()) && null != zsData.getData() && !zsData.getData().getPackage_order().isEmpty()) {
                                //先声明一个list集合用来装符合未启用或暂停订单条件，并且与请求的套餐编码一致的订单
                                List<H5PackageOrder> finishList = new ArrayList<>();
                                for (H5PackageOrder t : zsData.getData().getPackage_order()) {
                                    //只允许符合条件的订单进入
                                    boolean b1 = StringUtils.isNotBlank(t.getPackageCode()) && StringUtils.isNotBlank(t.getOrderCode()) && StringUtils.isNotBlank(pur.getPackageCode()) && (t.getOrderStatus().equals(StringOutputConstant.CODE_ONE) || t.getOrderStatus().equals(StringOutputConstant.CODE_FIVE));
                                    if (b1 && pur.getPackageCode().equals(t.getPackageCode())) {
                                        //把所有订单中符合b1条件，并且符合套餐编码一致的前提的订单add进入到finishList以便去做排序等操作
                                        finishList.add(t);
                                    }
                                }
                                //如果有正在使用订单，再匹配未启用或暂停订单时，finishList集合不为空集合
                                if (!finishList.isEmpty()) {
                                    //这边判断一下时间先后再决定续订哪一单
                                    List<H5PackageOrder> rList = finishList;
                                    //获得排序后的订单列表
                                    rList = purchaseService.sort(rList);
                                    H5PackageOrder ta = rList.get(0);
                                    log.info(pur.getDeviceCode() + "：有正在使用单，但与未启用或暂停订单code一致：【续订】");
                                    RenewResponse reNew = toBeijingService.reOrder(ta.getOrderCode(), pur);
                                    if (null != reNew && Constant.RET_CODE_0000.equals(reNew.getCode())) {
                                        //去数据库更新isRenew和isOpen参数
                                        Integer c = assistService.updateOrderByOrderNo(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo().trim(), "1", "1");
                                        if (c > 0) {
                                            return assistService.responseStr("0000", pur.getDeviceCode() + "：有正在使用订单，同时请求【续订】成功");
                                        } else {
                                            //发送异常邮件：
                                            purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订未启用或者暂停单", "有正在使用订单，同时请求【续订】成功，但入库失败！");
                                            return assistService.responseStr("9999", pur.getDeviceCode() + "：有正在使用订单，同时请求【续订】成功，但入库失败！");
                                        }
                                    } else {
                                        if (null == reNew) {
                                            //发送异常邮件：
                                            purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订未启用或者暂停订单", "有正在使用订单，同时请求【续订】程序中异常导致的失败！");
                                            assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "2", "9998", "程序中异常导致的失败");
                                            return assistService.responseStr("9998", pur.getDeviceCode() + "：有正在使用订单，同时请求【续订】失败");
                                        } else {
                                            if (StringUtils.isNotBlank(reNew.getCode())) {
                                                //发送异常邮件：
                                                purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订未启用或者暂停订单", reNew.getMessage());
                                                assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "2", reNew.getCode(), reNew.getMessage());
                                                return assistService.responseStr(reNew.getCode(), pur.getDeviceCode() + ":" + reNew.getMessage());
                                            } else {
                                                //发送异常邮件：
                                                purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订未启用或者暂停订单", "有正在使用订单，同时请求【续订】程序中异常导致的失败！");
                                                assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "2", "9998", "程序中异常导致的失败");
                                                return assistService.responseStr("9998", pur.getDeviceCode() + "：有正在使用订单，同时请求【续订】失败");
                                            }
                                        }
                                    }
                                } else {
                                    log.info(pur.getDeviceCode() + "：符合有正在使用单同时【订购但不立即开通】");
                                    OrderResponse orderNew = toBeijingService.order(pur, "0");
                                    if (null != orderNew && Constant.RET_CODE_0000.equals(orderNew.getCode())) {
                                        //去数据库更新isRenew和isOpen参数
                                        Integer c = assistService.updateOrderByOrderNo(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "0", "0");
                                        if (c > 0) {
                                            return assistService.responseStr("0000", pur.getDeviceCode() + "：有正在使用订单同时【新订但不立即开通】成功");
                                        } else {
                                            //发送异常邮件：
                                            purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "新订但不立即开通", "有正在使用订单同时【新订但不立即开通】成功，但入库失败");
                                            return assistService.responseStr("9999", pur.getDeviceCode() + "：有正在使用订单同时【新订但不立即开通】成功，但入库失败");
                                        }
                                    } else {
                                        //这边需要做异常处理：
                                        if (null == orderNew) {
                                            //发送异常邮件：
                                            purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "新订不立即开通", "有正在使用单同时【新订但不立即开通】程序中异常导致的失败");
                                            assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                                            return assistService.responseStr("9998", pur.getDeviceCode() + "：【新订但不立即开通】失败");
                                        } else {
                                            if (StringUtils.isNotBlank(orderNew.getCode())) {
                                                //发送异常邮件：
                                                purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "新订不立即开通", orderNew.getMessage());
                                                assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", orderNew.getCode(), orderNew.getMessage());
                                                return assistService.responseStr(orderNew.getCode(), pur.getDeviceCode() + ":" + orderNew.getMessage());
                                            } else {
                                                //发送异常邮件：
                                                purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "新订不立即开通", "有正在使用订单同时【新订但不立即开通】程序中异常导致的失败");
                                                assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                                                return assistService.responseStr("9998", pur.getDeviceCode() + "：【新订不立即开通】失败");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //无正在使用的订单存在，继续去查询是不是存在相同套餐的未启用订单或者暂停订单，如果有按照时间最早续订。
            log.info(pur.getDeviceCode() + "：无【正在使用】订单，匹配【未启用或暂停订单】");
            if (null != zsData && Constant.RET_CODE_0000.equals(zsData.getCode()) && null != zsData.getData() && null != zsData.getData().getPackage_order() && !zsData.getData().getPackage_order().isEmpty()) {
                //先声明一个list集合用来装符合未启用或暂停订单条件，并且与请求的套餐编码一致的订单
                List<H5PackageOrder> finishList = new ArrayList<>();
                for (H5PackageOrder t : zsData.getData().getPackage_order()) {
                    boolean b1 = (StringUtils.isNotBlank(t.getPackageCode()) && StringUtils.isNotBlank(t.getOrderCode()) && StringUtils.isNotBlank(pur.getPackageCode()) && (t.getOrderStatus().equals(StringOutputConstant.CODE_ONE) || t.getOrderStatus().equals(StringOutputConstant.CODE_FIVE)));
                    if (b1 && pur.getPackageCode().equalsIgnoreCase(t.getPackageCode())) {
                        //把所有订单中符合b1条件，并且符合套餐编码一致的前提的订单add进入到finishList以便去做排序等操作
                        finishList.add(t);
                    }
                }
                //如果无正在使用订单，再匹配未启用或暂停订单时，finishList集合不为空集合
                if (!finishList.isEmpty()) {
                    //这边判断一下时间先后再决定续订哪一单
                    List<H5PackageOrder> rList = finishList;
                    //获得排序后的订单列表
                    rList = purchaseService.sort(rList);
                    H5PackageOrder ta = rList.get(0);
                    log.info(pur.getDeviceCode() + "：无正在使用订单，存在未启用或者暂停订单code一致：【续订】");
                    RenewResponse reNew = toBeijingService.reOrder(ta.getOrderCode(), pur);
                    if (null != reNew && Constant.RET_CODE_0000.equals(reNew.getCode())) {
                        //去数据库更新isRenew和isOpen参数
                        Integer c = assistService.updateOrderByOrderNo(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo().trim(), "1", "1");
                        if (c > 0) {
                            return assistService.responseStr("0000", pur.getDeviceCode() + "：无正在使用订单，存在未启用或者暂停订单code一致，【续订】成功");
                        } else {
                            //发送异常邮件：
                            purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订未启用或者暂停订单", "无正在使用单，存在未启用或者暂停订单code一致，【续订】成功,但入库失败");
                            return assistService.responseStr("9999", pur.getDeviceCode() + "：无正在使用订单，存在未启用或者暂停订单code一致，【续订】成功,但入库失败");
                        }
                    } else {
                        if (null == reNew) {
                            //发送异常邮件：
                            purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订未启用或者暂停单", "无正在使用单，存在未启用或者暂停订单code一致，【续订】程序中异常导致的失败");
                            assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "2", "9998", "程序中异常导致的失败");
                            return assistService.responseStr("9998", pur.getDeviceCode() + "：无正在使用单，存在未启用或者暂停订单code一致，【续订】失败");
                        } else {
                            if (StringUtils.isNotBlank(reNew.getCode())) {
                                //发送异常邮件：
                                purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订未启用或者暂停单", reNew.getMessage());
                                assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "2", reNew.getCode(), reNew.getMessage());
                                return assistService.responseStr(reNew.getCode(), pur.getDeviceCode() + ":" + reNew.getMessage());
                            } else {
                                //发送异常邮件：
                                purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "续订未启用或者暂停单", "无正在使用单，存在未启用或者暂停订单code一致，【续订】程序中异常导致的失败");
                                assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "2", "9998", "程序中异常导致的失败");
                                return assistService.responseStr("9998", pur.getDeviceCode() + "：无正在使用单，存在未启用或者暂停订单code一致，【续订】失败");
                            }
                        }
                    }
                }
            }
        }
        //如果以上所有情况都不满足，那么就只能符合新订的流程了，给上一级返回一个“SUCCESS”让程序继续往下走
        log.info(pur.getDeviceCode() + "：符合新订流程");
        return StringOutputConstant.CODE_SUCCESS;
    }

    /**
     * 旧的：匹配新订套餐的方法：
     * <p>
     * 需要根据设备确定新订要不要立即开通
     *
     * @param zsData
     * @param pur
     */
    public String matchOrder(ResponseQueryH5Order zsData, RequestPurchaseService pur) {

        //续订规则都走不通时，走新订,新订要不要开通中日程类套餐是个特例。这边对日程类套餐进行特殊处理，不允许订购并立即开通,订购之后也不需要手动开通
        if (StringUtils.isNotBlank(pur.getPriceType()) && StringUtils.isBlank(pur.getBuyType())) {
            log.info(pur.getDeviceCode() + "：日程类套餐订购且不允许立即开通:" + pur.getPriceType());
            OrderResponse orderNew = toBeijingService.order(pur, "0");
            if (null != orderNew && Constant.RET_CODE_0000.equals(orderNew.getCode())) {
                //去数据库更新isRenew和isOpen参数
                Integer c = assistService.updateOrderByOrderNo(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo().trim(), "0", "1");
                if (c > 0) {
                    return assistService.responseStr("0000", pur.getDeviceCode() + "：日程类套餐【订购不立即开通】成功");
                } else {
                    //发送异常邮件：
                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "日程类套餐：【订购不立即开通】", "日程类套餐：【订购不立即开通】成功，但入库失败");
                    return assistService.responseStr("9999", pur.getDeviceCode() + "：日程类套餐：【订购不立即开通】成功，但入库失败");
                }
            } else {
                //这边需要做异常处理：
                if (null == orderNew) {
                    //发送异常邮件：
                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "日程类套餐【订购不立即开通】", "日程类套餐：【订购不立即开通】程序中异常导致的失败");
                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                    return assistService.responseStr("9998", pur.getDeviceCode() + "：日程类套餐：【订购不立即开通】失败");
                } else {
                    if (StringUtils.isNotBlank(orderNew.getCode())) {
                        //发送异常邮件：
                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "日程类套餐【订购不立即开通】", orderNew.getMessage());
                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", orderNew.getCode(), orderNew.getMessage());
                        return assistService.responseStr(orderNew.getCode(), pur.getDeviceCode() + ":" + orderNew.getMessage());
                    } else {
                        //发送异常邮件：
                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "日程类套餐【订购不立即开通】", "日程类套餐：【订购不立即开通】程序中异常导致的失败");
                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                        return assistService.responseStr("9998", pur.getDeviceCode() + "：日程类套餐：【订购不立即开通】失败");
                    }
                }
            }
        }

        log.info(pur.getDeviceCode() + "：流量套餐新订逻辑：" + pur.getBuyType());
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        //续订规则都走不通时，走新订，但是流量套餐新订时要不要立即开通要根据是否为OTA决定：在这边先判断一下卡片是ota还是非ota卡片
        //对于卡片上预置就是商用套餐购买新套餐原套餐的状态不变。
        ResponseQueryDevice device = toBeijingService.queryDevice(pur.getDeviceCode().trim(), pur.getPartnerCode(), pur.getSecret());

        if (null != device && Constant.RET_CODE_0000.equals(device.getCode()) && null != device.getData()) {

            QueryDeviceData tr = device.getData();

            if (StringUtils.isNotBlank(tr.getOta_flag()) && StringUtils.isNotBlank(tr.getDevice_id())) {

                //现在开始判断首单及状态
                if (null != zsData && Constant.RET_CODE_0000.equals(zsData.getCode()) && null != zsData.getData() && null != zsData.getData().getPackage_order() && !zsData.getData().getPackage_order().isEmpty()) {
                    boolean b1 = false;
                    zsData:
                    for (H5PackageOrder t : zsData.getData().getPackage_order()) {
                        boolean b2 = StringUtils.isNotBlank(t.getPackageCode()) && StringUtils.isNotBlank(t.getOrderCode()) &&
                                StringUtils.isNotBlank(t.getOrderStatus()) && StringUtils.isNotBlank(t.getIsFirstOrder()) && t.getIsFirstOrder().equals(StringOutputConstant.CODE_ONE);
                        //此条件查询到了卡片上面的首单，下面判断首单是不是赠送套餐
                        if (b2) {
                            //拿着t.getPackageCode()去数据库查询看看有没有这个赠送套餐的套餐编码
                            Integer c = purchaseService.queryGiveDateOrNot(t.getPackageCode());
                            log.info(t.getPackageCode() + "：作为首单匹配【赠送套餐列】，结果是：" + c);
                            //首单不是赠送套餐
                            boolean b3 = c < 1;
                            //意味着这个套餐不是赠送套餐,list2数组的是商用套餐,list1是赠送套餐
                            if (b3) {
                                list2.add("0");
                            } else {
                                list1.add("1");
                            }
                            //2020-5-13修改：原来判断在B2平级，放到里面首先确认订单是首单，才好判断：这边判断卡片首单的状态是不是已取消或者已结束
                            b1 = StringUtils.isNotBlank(t.getPackageCode()) && StringUtils.isNotBlank(t.getOrderCode()) && StringUtils.isNotBlank(t.getOrderStatus()) && StringUtils.isNotBlank(t.getIsFirstOrder()) &&
                                    t.getIsFirstOrder().equals(StringOutputConstant.CODE_ONE) && (t.getOrderStatus().equals(StringOutputConstant.CODE_FOUR) || t.getOrderStatus().equals(StringOutputConstant.CODE_THREE));
                            //2020-7-14,当卡片是多订单的情况时，当我们拿到符合是首单同时首单又是已取消或者已结束时，要跳出循环，不能继续循环，继续循环的结果就不是当前首单了。
                            if (b1) {
                                //当匹配到这种情况的订单就要跳出循环，否则循环还将继续
                                break zsData;
                            }
                        }
                    }
                    //符合首单为商用套餐或订购日程类套餐情况
                    boolean b4 = ((!list2.isEmpty() && list1.isEmpty()) || StringUtils.isNotBlank(pur.getPriceType()));
                    if (b4) {
                        if (b1 && StringOutputConstant.CODE_TWO.equals(tr.getOta_flag())) {
                            //首单为商用套餐且状态为已取消或者已结束，并且卡片为非ota卡==订购并且立即开通
                            log.info(pur.getDeviceCode() + "：符合商用首单：已取消或已结束且为非OTA卡【订购并立即开通】");
                            OrderResponse orderNew = toBeijingService.order(pur, "1");
                            if (null != orderNew && Constant.RET_CODE_0000.equals(orderNew.getCode())) {
                                //去数据库更新isRenew和isOpen参数
                                Integer c = assistService.updateOrderByOrderNo(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo().trim(), "0", "1");
                                if (c > 0) {
                                    return assistService.responseStr("0000", pur.getDeviceCode() + "：商用首单：已取消或已结束且为非OTA卡【订购并立即开通】成功");
                                } else {
                                    //发送异常邮件：
                                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "商用首单：【订购并立即开通】", "商用首单：已取消或已结束且为非OTA卡【订购并立即开通】成功，但入库失败");
                                    return assistService.responseStr("9999", pur.getDeviceCode() + "：商用首单：已取消或已结束且为非OTA卡【订购并立即开通】成功，但入库失败");
                                }
                            } else {
                                //这边需要做异常处理：
                                if (null == orderNew) {
                                    //发送异常邮件：
                                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "商用首单：【订购并立即开通】", "商用首单：已取消或已结束且为非OTA卡【订购并立即开通】程序中异常导致的失败");
                                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                                    return assistService.responseStr("9998", pur.getDeviceCode() + "：商用首单：已取消或已结束且为非OTA卡【订购并立即开通】失败");
                                } else {
                                    if (StringUtils.isNotBlank(orderNew.getCode())) {
                                        //发送异常邮件：
                                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "商用首单：【订购并立即开通】", orderNew.getMessage());
                                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", orderNew.getCode(), orderNew.getMessage());
                                        return assistService.responseStr(orderNew.getCode(), pur.getDeviceCode() + ":" + orderNew.getMessage());
                                    } else {
                                        //发送异常邮件：
                                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "商用首单：【订购并立即开通】", "商用首单：已取消或已结束且为非OTA卡【订购并立即开通】程序中异常导致的失败");
                                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                                        return assistService.responseStr("9998", pur.getDeviceCode() + "：商用首单：已取消或已结束且非OTA卡【订购并立即开通】失败");
                                    }
                                }
                            }
                        } else {//否则==订购但不立即开通
                            log.info(pur.getDeviceCode() + "：符合商用首单：状态正常或OTA卡【订购不立即开通】");
                            OrderResponse orderNew = toBeijingService.order(pur, "0");
                            if (null != orderNew && Constant.RET_CODE_0000.equals(orderNew.getCode())) {
                                //去数据库更新isRenew和isOpen参数 （ota的卡片接口永远起不来，所以这边直接设为不用启用的1）
                                Integer c = assistService.updateOrderByOrderNo(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo().trim(), "0", "0");
                                if (c > 0) {
                                    return assistService.responseStr("0000", pur.getDeviceCode() + "：符合商用首单：状态正常或为OTA卡【订购不立即开通】成功");
                                } else {
                                    //发送异常邮件：
                                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "商用首单：【订购不立即开通】", "符合商用首单：状态正常或为OTA卡【订购不立即开通】成功，但入库失败");
                                    return assistService.responseStr("9999", pur.getDeviceCode() + "：符合商用首单：状态正常或为OTA卡【订购不立即开通】成功，但入库失败");
                                }
                            } else {
                                //这边需要做异常处理：
                                if (null == orderNew) {
                                    //发送异常邮件：
                                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "商用首单：【订购不立即开通】", "符合商用首单：状态正常或为OTA卡【订购不立即开通】程序中异常导致的失败");
                                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                                    return assistService.responseStr("9998", pur.getDeviceCode() + "：符合商用首单：状态正常或OTA卡【订购不立即开通】失败");
                                } else {
                                    if (StringUtils.isNotBlank(orderNew.getCode())) {
                                        //发送异常邮件：
                                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "商用首单：【订购不立即开通】", orderNew.getMessage());
                                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", orderNew.getCode(), orderNew.getMessage());
                                        return assistService.responseStr(orderNew.getCode(), pur.getDeviceCode() + ":" + orderNew.getMessage());
                                    } else {
                                        //发送异常邮件：
                                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "商用首单：【订购不立即开通】", "符合商用首单：状态正常或为OTA卡【订购不立即开通】程序中异常导致的失败");
                                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                                        return assistService.responseStr("9998", pur.getDeviceCode() + "：符合商用首单：状态正常或OTA卡【订购不立即开通】失败");
                                    }
                                }
                            }
                        }
                    } else {//otaFlag:是否有OTA功能，取值如下：1-有OTA功能；2-没有OTA功能
                        if (StringOutputConstant.CODE_ONE.equals(tr.getOta_flag())) {
                            log.info(pur.getDeviceCode() + "：符合首单为赠送套餐或为OTA卡【订购不立即开通】");
                            OrderResponse orderNew = toBeijingService.order(pur, "0");
                            if (null != orderNew && Constant.RET_CODE_0000.equals(orderNew.getCode())) {
                                //去数据库更新isRenew和isOpen参数 （ota的卡片接口永远起不来，所以这边直接设为不用启用的1）
                                Integer c = assistService.updateOrderByOrderNo(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo().trim(), "0", "0");
                                if (c > 0) {
                                    return assistService.responseStr("0000", pur.getDeviceCode() + "：首单为赠送套餐或为OTA卡【订购不立即开通】成功");
                                } else {
                                    //发送异常邮件：
                                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "赠送首单：【订购不立即开通】", "首单为赠送套餐或为OTA卡【订购不立即开通】成功，但入库失败");
                                    return assistService.responseStr("9999", pur.getDeviceCode() + "：首单为赠送套餐或为OTA卡【订购不立即开通】成功，但入库失败");
                                }
                            } else {
                                //这边需要做异常处理：
                                if (null == orderNew) {
                                    //发送异常邮件：
                                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "赠送首单：【订购不立即开通】", "首单为赠送套餐或为OTA卡【订购不立即开通】程序中异常导致的失败");
                                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                                    return assistService.responseStr("9998", pur.getDeviceCode() + "：首单为赠送套餐或OTA卡【订购不立即开通】失败");
                                } else {
                                    if (StringUtils.isNotBlank(orderNew.getCode())) {
                                        //发送异常邮件：
                                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "赠送首单：【订购不立即开通】", orderNew.getMessage());
                                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", orderNew.getCode(), orderNew.getMessage());
                                        return assistService.responseStr(orderNew.getCode(), pur.getDeviceCode() + ":" + orderNew.getMessage());
                                    } else {
                                        //发送异常邮件：
                                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "赠送首单：【订购不立即开通】", "首单为赠送套餐或为OTA卡【订购不立即开通】程序中异常导致的失败");
                                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                                        return assistService.responseStr("9998", pur.getDeviceCode() + "：首单为赠送套餐或OTA卡【订购不立即开通】失败");
                                    }
                                }
                            }
                        } else {
                            log.info(pur.getDeviceCode() + "：符合首单为赠送套餐非OTA卡【订购并立即开通】");
                            OrderResponse orderNew = toBeijingService.order(pur, "1");
                            if (null != orderNew && Constant.RET_CODE_0000.equals(orderNew.getCode())) {
                                //去数据库更新isRenew和isOpen参数
                                Integer c = assistService.updateOrderByOrderNo(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo().trim(), "0", "1");
                                if (c > 0) {
                                    return assistService.responseStr("0000", pur.getDeviceCode() + "：首单为赠送套餐为非OTA卡【订购并立即开通】成功");
                                } else {
                                    //发送异常邮件：
                                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "赠送首单：【订购并立即开通】", "首单为赠送套餐为非OTA卡【订购并立即开通】成功，但入库失败");
                                    return assistService.responseStr("9999", pur.getDeviceCode() + "：首单为赠送套餐为非OTA卡【订购并立即开通】成功，但入库失败");
                                }
                            } else {
                                //这边需要做异常处理：
                                if (null == orderNew) {
                                    //发送异常邮件：
                                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "赠送首单：【订购并立即开通】", "首单为赠送套餐为非OTA卡【订购并立即开通】程序中异常导致的失败");
                                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                                    return assistService.responseStr("9998", pur.getDeviceCode() + "：首单为赠送套餐非OTA卡【订购并立即开通】失败");
                                } else {
                                    if (StringUtils.isNotBlank(orderNew.getCode())) {
                                        //发送异常邮件：
                                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "赠送首单：【订购并立即开通】", orderNew.getMessage());
                                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", orderNew.getCode(), orderNew.getMessage());
                                        return assistService.responseStr(orderNew.getCode(), pur.getDeviceCode() + ":" + orderNew.getMessage());
                                    } else {
                                        //发送异常邮件：
                                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "赠送首单：【订购并立即开通】", "首单为赠送套餐为非OTA卡【订购并立即开通】程序中异常导致的失败");
                                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "1", "9998", "程序中异常导致的失败");
                                        return assistService.responseStr("9998", pur.getDeviceCode() + "：首单为赠送套餐非OTA卡【订购并立即开通】失败");
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (null == zsData) {
                        //发送异常邮件：
                        purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "查询全部订单", "【匹配全部订单及状态】程序中异常导致的失败");
                        assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "5", "9998", "程序中异常导致的失败");
                        return assistService.responseStr("9998", pur.getDeviceCode() + "：【查询全部订单及状态】失败，程序终止！");
                    } else {
                        if (StringUtils.isNotBlank(zsData.getCode())) {
                            //发送异常邮件：
                            purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "查询全部订单", zsData.getMessage());
                            assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "5", zsData.getCode(), zsData.getMessage());
                            return assistService.responseStr(zsData.getCode(), pur.getDeviceCode() + ":" + zsData.getMessage());
                        } else {
                            //发送异常邮件：
                            purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "查询全部订单", "【匹配全部订单及状态】程序中异常导致的失败");
                            assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "5", "9998", "程序中异常导致的失败");
                            return assistService.responseStr("9998", pur.getDeviceCode() + "：【查询全部订单及】失败，程序终止！");
                        }
                    }
                }
            } else {
                //发送异常邮件：
                purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "查询设备是否为OTA", "【查询设备是否为OTA】程序响应了但是主要的参数为空导致了查询失败");
                assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "5", "9998", "程序响应了但是主要的参数为空导致了查询失败");
                return assistService.responseStr("9998", pur.getDeviceCode() + "：【查询设备是否为OTA】返回参数有误，程序终止！");
            }

        } else {
            if (null == device) {
                //发送异常邮件：
                purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "查询设备是否为OTA卡", "【查询设备是否为OTA】程序中异常导致的失败");
                assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "5", "9998", "程序中异常导致的失败");
                return assistService.responseStr("9998", pur.getDeviceCode() + "：【查询设备是否为OTA卡】失败，程序终止！");
            } else {
                if (StringUtils.isNotBlank(device.getCode())) {
                    //发送异常邮件：
                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "查询设备是否为OTA卡", device.getMessage());
                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "5", device.getCode(), device.getMessage());
                    return assistService.responseStr(device.getCode(), pur.getDeviceCode() + ":" + device.getMessage());
                } else {
                    //发送异常邮件：
                    purchaseService.sendMailToPrivate(pur.getDeviceCode(), pur.getPackageName(), "查询设备是否为OTA卡", "【查询设备是否为OTA】程序中异常导致的失败");
                    assistService.exceptionDetailsRecord(pur.getPartnerCode(), pur.getDeviceCode().trim(), pur.getPayId().trim(), pur.getOrderNo(), "5", "9998", "程序中异常导致的失败");
                    return assistService.responseStr("9998", pur.getDeviceCode() + "：【查询设备是否是OTA卡】失败，程序终止！");
                }
            }
        }
    }

}
